﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace WebControl
{
    ///<summary>
    /// 脚本控件
    ///</summary>
    public class Script : TemplateControl, INamingContainer
    {
        private string _src = "";
        private string _webPath = "";
        private StringBuilder result;
        private bool _isScript = true;
        private string _jsformat = "<script type='text/javascript' src='{0}{1}'></script>\n";
        private string _cssFormat = "<link href=\"{0}{1}\" rel=\"stylesheet\" type=\"text/css\" /> \n";


        public Script()
        {
            result = new StringBuilder("");
        }

        /// <summary>
        /// 脚本文件
        /// </summary>
        public string Src
        {
            set { _src = value; }
            get { return _src; }
        }

        /// <summary>
        /// js所在网站路径
        /// </summary>
        public string WebPath
        {
            set { _webPath = value; }
            get{
                return _webPath;
            }
        }



        /// <summary>
        /// 呈现事件
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            string[] strs = _src.Split(new []{"|"}, StringSplitOptions.RemoveEmptyEntries);
            if (0 == strs.Length)
            {
                writer.Write("");
                return;
            }
            if(!String.IsNullOrEmpty(_webPath))
            {
                _webPath = _webPath + @"/" ;
            }
            result.Append("\n");
            for (int i = 0, j = strs.Length; i < j; i++)
            {
                string temp = strs[i].Trim();
                if (temp.ToLower().EndsWith("js"))
                    result.AppendFormat(_jsformat, _webPath, strs[i].Trim());
                else if (temp.ToLower().EndsWith("css"))
                    result.AppendFormat(_cssFormat, _webPath, strs[i].Trim());
            }
            writer.Write(result.ToString());
        }

    }
}
